package com.treasure.mall.biz.repository.integral;

import com.treasure.mall.biz.base.JpaSearchableRepository;
import com.treasure.mall.biz.entity.integral.IntegralProductPO;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/8/22
 */
@Repository
public interface IntegralProductRepository extends JpaSearchableRepository<IntegralProductPO, Long> {
    /**
     * 减少库存
     *
     * @param productId
     * @param store
     * @param version
     * @return
     */
    @Query("update IntegralProductPO po set po.store=po.store-?2,po.version=po.version+1 " +
            "where po.version=?3 and po.store-?2>=0 and po.id=?1")
    @Modifying
    @Transactional
    int minusStore(Long productId, int store, long version);
}
